const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');

Page({
  data: {
    terminalId: -1, //设备Id
    areas: [], //存放区域
    hTemp: 0, //高温
    lTemp: 0, //低温
    area: '', //选中的区域
  },

  onLoad: function (options) {
    this.areaList()
    this.setData({
      terminalId: options.terminalId
    })
    this.queryAlarmSetByterminalId()
  },

  onShow: function () {},

  highInput: function (e) {
    this.setData({
      hTemp: e.detail.value
    })
  },

  lowInput: function (e) {
    this.setData({
      lTemp: e.detail.value
    })
  },

  radioGroupChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      area: e.detail.value
    })
  },


  // 提交
  submit: function () {
    let that = this
    wx.showModal({
      title: '提示',
      content: '您确定提交设置吗',
      confirmText: '提交',
      success(res) {
        if (res.confirm) {
          that.coldDeviceAlarmSet()
        }
      }
    })

  },


  //数据回显
  queryAlarmSetByterminalId: function () {

    let data = {
      terminalId: this.data.terminalId
    }
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.queryAlarmSetByterminalId,
      data: data,
      fn: function (res) {
        wx.hideLoading()
        if (res.code == 0) {
          if (res.data != null) {
            that.setData({
              area: res.data.placeForStorage,
              hTemp: res.data.hTemp,
              lTemp: res.data.lTemp
            })
          }
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

  //存放区域
  areaList: function () {

    let data = {}
    const that = this
    wxHttpHandler({
      type: 'post',
      path: api.areaList,
      data: data,
      fn: function (res) {
        wx.hideLoading()
        if (res.code == 0) {
          if (res.data != null) {
            that.setData({
              areas: res.data
            })
          }
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //报警设置
  coldDeviceAlarmSet: function () {

    if (this.data.area == '') {
      wx.showToast({
        title: '请选择存放区域',
      })
      return
    }

    if (this.data.hTemp < this.data.lTemp) {
      wx.showToast({
        title: '最高温不能低于最低温',
      })
      return
    }


    let data = {
      terminalId: this.data.terminalId,
      placeForStorage: this.data.area,
      hTemp: this.data.hTemp,
      lTemp: this.data.lTemp
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.coldDeviceAlarmSet,
      data: data,
      fn: function (res) {
        wx.hideLoading()
        if (res.code == 0) {
          wx.showToast({
            title: '已提交',
          })
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },
})